﻿using Jesai.DynamicDataSource.Sqlsugar.Abstractions;
using Jesai.DynamicDataSource.Sqlsugar.Builder;
using Jesai.DynamicDataSource.Sqlsugar.DynamicDatasources;
using Jesai.DynamicDataSource.Sqlsugar.Options;
using JESAI.Core.Configuration;
using JESAI.Data.Filters;
using JESAI.Datas.Sqlsugar.Extensions;
using JESAI.Datas.Sqlsugar.Filters;
using JESAI.Datas.Sqlsugar.Options;
using JESAI.Datas.Sqlsugar.Repositories;
using JESAI.Datas.Sqlsugar.Uow;
using JESAI.DDD.Repositories;
using JESAI.Uow;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MySqlConnector;
using System;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        public static void UseSqlsugarDynamicDataSource(this IApplicationBuilder app)
        {

        }

        public static IServiceCollection AddSqlsugarDynamicDataSource(this IServiceCollection services)
        {
            IConfiguration configuration = services.BuildServiceProvider().GetService<IConfiguration>().GetSection("MonacoRoslynComplete");
            //services.TryAddSingleton<IDbProvider, DbProvider>();
            //判断是否已经注入IDBConnectionProvider
            if (!services.Any(x => x.ServiceType == typeof(IDBConnectionProvider)))
            {
                services.TryAddSingleton<IDBConnectionProvider, DefaultDBConnectionProvider>();
            }
            IDynamicDataSourceBuilder builder = new DefaultDynamicDataSourceBuilder(services, configuration);
            builder.UseSqlsugarDynamicDataSource().InitDynamicDataSource();
            return services;
        }

        public static IDynamicDataSourceBuilder UseSqlsugarDynamicDataSource(this IDynamicDataSourceBuilder builder)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<DynamicDataSourceOptions>(builder.Configuration.GetSection("DynamicSource"));
            return builder;
        }

        public static IDynamicDataSourceBuilder UseSqlsugarDynamicDataSource(this IDynamicDataSourceBuilder builder, Action<DynamicDataSourceOptions> options)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<DynamicDataSourceOptions>(options);

            return builder;
        }


        public static IDynamicDataSourceBuilder InitDynamicDataSource(this IDynamicDataSourceBuilder builder)
        {
            //var provier = builder.Services.BuildServiceProvider().GetService<IDbProvider>() ?? throw new ArgumentNullException("Provier Service Not Found");
            var dbConnectionProvider = builder.Services.BuildServiceProvider().GetService<IDBConnectionProvider>() ?? throw new ArgumentNullException("DBConnectionProvider Service Not Found");
            //如何使得GetDbConnections 同步执行
            var conns = dbConnectionProvider.GetDbConnections().Result;
            var provider = new DbProvider();
            provider.InitDataSourceAsync(conns).Wait();
            builder.Services.AddSingleton<IDbProvider>(provider);
            return builder;
        }


    }
}